package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/double-a-number-represented-as-a-linked-list/description/
 * @create 2025/9/27 20:45
 * @since JDK17
 */

public class Solution75 {
    ListNode reverse(ListNode head) {
        ListNode pre = null, cur = head;
        while (cur != null) {
            var next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public ListNode doubleIt(ListNode head) {
        head = reverse(head);
        ListNode h = head, pre = null;
        int last = 0;
        while (head != null) {
            head.val = head.val * 2 + last;
            last = head.val / 10;
            head.val %= 10;
            pre = head;
            head = head.next;
        }
        if (last != 0) {
            pre.next = new ListNode(last);
        }
        return reverse(h);
    }
}
